package com.qf.smsplatform.deadletter.controller;

import com.qf.smsplatform.deadletter.config.DelayedConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DelayedController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/delayed")
    public String delayed(String msg,Integer delayed){
        rabbitTemplate.convertAndSend(DelayedConfig.DELAYED_EXCHANGE, "aaa.bbb.ccc", msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayed);
                return message;
            }
        });
        System.out.println(System.currentTimeMillis() + "消息发送完毕!");
        return "SUCCESS";
    }


}
